<?php

namespace app\common\cachemodel;

use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class AppCompensationGiveRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_compensation_give_record';

    protected $cacheModel = 'active.compensation_give_record';


    /**
     * 添加 领取补偿记录
     * @param array $data
     * @return int
     */
    public function saveData(array $data)
    {
        try
        {
            $id = $this->insertGetId($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $data['uid'])->rmCache();
            return (int)$id;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return 0;
        }
    }

    /**
     * 获取 用户领取补偿状态
     * @param int $uid
     * @return bool
     * @throws \Exception
     */
    public function isGived(int $uid)
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)
            ->setCacheType('string')
            ->getDataAndSet(function () use ($uid)
            {
                try
                {
                    $num = $this->where(['uid' => $uid])->count();
                    return $num;
                }
                catch (\Exception $e)
                {
                    \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
                    return 0;
                }
            });

        return $data ? true : false;
    }
}